package com.hellogood.yaohuo.server.mgr;

import java.util.List;

import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.server.mgr.factory.MgrFactory;
import com.hellogood.yaohuo.shared.entity.Group;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class GroupMgr {
	public List<Group> getAllGroup() {
		return DAOFactory.getInstance().createGroupDAO().getAllGroup();
	}

	public String addNewGroup(Group g) {
		Group temp = DAOFactory.getInstance().createGroupDAO().getGroupByCode(g.getGroupCode());
		if (temp == null) {
			String result = DAOFactory.getInstance().createGroupDAO().addNewGroup(g);
			if (result.equals("")) {
				int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
				logID++;
				Log log = new Log();
				log.setStaffCode(MainFrame.currentUser.getStaffCode());
				log.setStaffName(MainFrame.currentUser.getStaffName());
				log.setLogId(logID);
				log.setLogDate(DateUtil.currentTime());
				log.setContent("新建分组：" + g.getGroupCode() + "-" + g.getGroupName());
				DAOFactory.getInstance().createLogDAO().addNewLog(log);
			}
			return result;
		} else {
			return "编码：" + g.getGroupCode() + " 已经存在!";
		}
	}

	public String removeGroup(String groupCode) {
		if (MgrFactory.getInstance().createGroupUserMgr().doesGroupHasOrg(groupCode)) {
			return "该用户组下含有用户，不能删除";
		} else {
			String result = DAOFactory.getInstance().createGroupDAO().removeGroup(groupCode);
			if (result.equals("")) {
				int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
				logID++;
				Log log = new Log();
				log.setLogId(logID);
				log.setStaffCode(MainFrame.currentUser.getStaffCode());
				log.setStaffName(MainFrame.currentUser.getStaffName());
				log.setLogDate(DateUtil.currentTime());
				log.setContent("删除分组：" + groupCode);
				DAOFactory.getInstance().createLogDAO().addNewLog(log);
			}
			return result;
		}
	}

}
